home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
MACSHELL
/
MS1
/
UTILS
/
FILEUTIL.C
< prev
next >
Wrap
Text File
|
1992-12-02
|
13KB
|
602 lines
/*
* MacShell Source File
*
* Copyright (c) 1989, 1990, 1991, 1992 Suick Bay Technologies. All rights reserved.
*
*
* RESTRICTIONS ON MacShell program and source code.
*
* Ñ╩MacShell¬ is a product of Suick Bay Technologies and is provided for
* restricted use by the owner of the CDROM "Disk to the future II".
*
* Ñ╩No permission is granted for any commercial use without the written
* consent of the Suick Bay Technologies.
*
* Ñ╩No permission is granted for any redistribution of any kind use without
* the written consent of the Suick Bay Technologies.
*
* Ñ╩Permission is granted to use this for any personal noncommercial use.
*
* Ñ╩You may not distribute source or executable code at all, nor may you
* distribute it with or within a commercial product without the written
* consent of the Suick Bay Technologies. Please send modifications to
* the author for inclusion in updates to the program. Thanks.
*
*
* MacShell¬ IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
* WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
* PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
*
* SUICK BAY TECHNOLOGIES SHALL HAVE NO LIABILITY WITH RESPECT TO THE
* INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY MACSHELL
* OR ANY PART THEREOF.
*
* In no event will Suick Bay Technologies be liable for any lost revenue
* or profits or other special, indirect and consequential damages, even if
* Suick Bay Technologies has been advised of the possibility of such damages.
*
* Suick Bay Technologies can be reached at:
*
* 8768 Cottonwood lane
* Maple Grove, MN 55369
* Voice: (612) 425-7025
* AppleLink: D5233
*
*
* No parts of this software may be reproduced or stored in a
* retrieval system or transmitted in any form, or any means,
* electronic, mechanical, photocopying, recording or otherwise,
* without the prior written permission of Suick Bay Technologies.
*
* Spread the word and not the disk.
*
* SPK 030190 : Update for Mac, DOS paths
* SPK 012290 : Initial
*/
#include "SystemPub.h"
#include "Proc.h"
#include "ShellPub.h"
#include "Path.h"
#include "Prefs.h"
#define BUFSIZE 512
Boolean abortFileOp = FALSE;
#define _DEBUG
/*******************************************************************/
char *ExtractFile( char *path )
{
char *cp, sep, *file;
if( ShellPrefs.useMacOSPath )
sep = ':';
else if( ShellPrefs.useUNIXPath )
sep = '/';
else if( ShellPrefs.useDOSPath )
sep = '\\';
cp = path;
file = cp;
while( *cp ) /* find the file name in the path */
if( *cp == sep )
file = ++cp;
else
cp++;
return( file );
}
/*******************************************************************/
OSErr ReadLine( int16 refNum, char *lineBuf, int32 bufSize )
{
int32 count;
OSErr err;
while( bufSize-- )
{
count = 1L;
err = FSRead( refNum, &count, lineBuf );
if( err || *lineBuf == '\n' || *lineBuf == '\r' )
{
*(++lineBuf) = '\0';
return( err );
}
else
lineBuf++;
}
return( noErr );
}
/*******************************************************************/
int16 OpenFileDirect( char *filePath, int32 fileType, int16 perm )
{
int16 pwdVRefNum = 0, err,
fileRefNum = 0;
char buf[ BUFSIZE ], *cp;
int32 parDirID, pwdDirID;
pathType pt;
FInfo finder;
strcpy( buf, filePath );
pt = SetCurrPath( buf );
if( pt == pathIsFile )
{
cp = GetLastScan();
strcpy( buf, cp );
CtoPstr( buf );
GetPWDInfo( &pwdVRefNum, &pwdDirID, &parDirID );
err = HGetFInfo( pwdVRefNum, pwdDirID, buf, &finder );
if( !err && (finder.fdType == fileType ))
err = HOpen( pwdVRefNum, pwdDirID, buf, perm, &fileRefNum );
if( err )
fileRefNum = 0;
}
return( fileRefNum );
}
/*******************************************************************/
char *ExtractPath( char *path, char *pbuf )
{
while( *path != ';' && *path )
*pbuf++ = *path++;
*pbuf = '\0';
if( *path )
path++;
return( path );
}
/*******************************************************************/
int16 OpenFileInPath( char *path, char *filePath, int32 fileType,
int16 perm )
{
int16 pwdVRefNum = 0, err,
fileRefNum = 0;
char pathbuf[ BUFSIZE ], buf[ BUFSIZE ], *cp;
int32 parDirID, pwdDirID;
pathType pt;
FInfo finder;
char *nextPath;
/*
* For each path spec in the PATH var
* try to open the file
*/
GetPWDInfo( &pwdVRefNum, &pwdDirID, &parDirID );
nextPath = path;
while( *nextPath )
{
nextPath = ExtractPath( nextPath, pathbuf );
/* if UNIX */
sprintf( buf, "%s/%s", pathbuf, filePath );
fileRefNum = OpenFileDirect( buf, fileType, perm );
if( fileRefNum )
{
strcpy( filePath, buf );
return( fileRefNum );
}
else
SetWD( pwdVRefNum, pwdDirID );
}
return( fileRefNum );
}
/*******************************************************************/
int16 OpenFile( WHandle ShellWh, char *filePath, int32 fileType,
int16 perm )
{
int16 fileRefNum = 0;
char *inPath;
fileRefNum = OpenFileDirect( filePath, fileType, perm );
if( fileRefNum )
return( fileRefNum );
inPath = ShellGetVar( ShellWh, "PATH" );
if( inPath )
return( OpenFileInPath( inPath, filePath, fileType, perm ) );
}
/*******************************************************************/
int16 CreateFile( char *filePath, int32 fileType )
{
int16 pwdVRefNum = -1, err;
char buf[ 64 ], path[ BUFSIZE ], *file;
int32 parDirID, pwdDirID;
pathType pt;
FInfo finder;
strcpy( path, filePath );
pt = SetCurrPath( path );
if( pt == pathIsFile )
strcpy( buf, GetLastScan() );
else
{
file = ExtractFile( path );
strcpy( buf, file ); /* we now have the file name */
if( file != path ) /* directory path found */
{
file--; /* set path */
*file = '\0';
pt = SetCurrPath( path ); /* look for dir */
}
}
GetPWDInfo( &pwdVRefNum, &pwdDirID, &parDirID );
CtoPstr( buf );
if( pt == pathIsFile ) /* file exists delete it */
{
err = HGetFInfo( pwdVRefNum, pwdDirID, buf, &finder );
if( !err )
{
if( finder.fdType == fileType )
err = HDelete( pwdVRefNum, pwdDirID, buf );
err = HCreate( pwdVRefNum, pwdDirID, buf, ShellPrefs.TextCreator, 'TEXT' );
}
}
else
err = HCreate( pwdVRefNum, pwdDirID, buf, ShellPrefs.TextCreator, 'TEXT' );
return( err );
}
/*******************************************************************/
OSErr RmTree( char *name, int16 vRefNum, int32 parDirID )
{
OSErr err;
int16 entries;
int32 dirID;
CInfoPBRec scanPB;
char scan[ 256 ];
/* get number of entries */
strcpy( scan, name );
CtoPstr( scan );
scanPB.dirInfo.ioFDirIndex = 0; /* name in this directory */
scanPB.dirInfo.ioCompletion = 0L;
scanPB.dirInfo.ioNamePtr = (StringPtr) scan;
scanPB.dirInfo.ioVRefNum = vRefNum;
scanPB.dirInfo.ioDrDirID = parDirID; /* parent specified */
err = PBGetCatInfo( &scanPB, FALSE );
if( err )
return( err );
entries = scanPB.dirInfo.ioDrNmFls + 1;
dirID = scanPB.dirInfo.ioDrDirID;
/*
* for each entry
* if a file then
* delete file
* else if a directory then
* ForceRmDir
*/
while( --entries )
{
CursorWait();
if( abortFileOp || UserAbort() )
{
abortFileOp = TRUE;
break;
}
scanPB.hFileInfo.ioFDirIndex = entries;
scanPB.hFileInfo.ioCompletion = 0L;
scanPB.hFileInfo.ioNamePtr = (StringPtr) scan;
scanPB.hFileInfo.ioVRefNum = vRefNum;
scanPB.hFileInfo.ioDirID = dirID;
err = PBGetCatInfo( &scanPB, FALSE );
if( err )
break;
if( scanPB.hFileInfo.ioFlAttrib & 0x10 ) /* was a directory */
{
if( DirIsEmpty( scan, vRefNum, dirID ) )
err = HDelete( vRefNum, dirID, scan );
else
{
PtoCstr( scan );
err = RmTree( scan, vRefNum, dirID );
}
}
else /* was a file */
err = HDelete( vRefNum, dirID, scan );
if( err )
break;
}
if( !err )
{
strcpy( scan, name );
CtoPstr( scan );
err = HDelete( vRefNum, parDirID, scan );
}
SetArrow();
return( err );
}
/*******************************************************************/
OSErr CopyFork( srcRefNum, dstRefNum )
{
int16 err;
char buf[ BUFSIZE ];
int32 bytesToRead,
fileLength;
err = noErr;
bytesToRead = BUFSIZE;
err = GetEOF( srcRefNum, &fileLength );
if( fileLength < bytesToRead )
bytesToRead = fileLength;
while( bytesToRead )
{
err = FSRead( srcRefNum, &bytesToRead, buf );
if( err )
break;
err = FSWrite( dstRefNum, &bytesToRead, buf );
if( err )
break;
fileLength -= bytesToRead;
if( fileLength < bytesToRead )
bytesToRead = fileLength;
if( bytesToRead < 0 )
bytesToRead = 0;
}
}
/*******************************************************************/
OSErr CopyFile( char *srcName, char *dstName,
int16 srcVRefNum, int16 dstVRefNum,
int32 srcDirID, int32 dstDirID )
{
FInfo finderInfo;
int16 err;
int16 srcRefNum = 0,
dstRefNum = 0;
int32 bytesToRead,
fileLength;
CtoPstr( srcName );
if( dstName != srcName )
CtoPstr( dstName );
err = HGetFInfo( srcVRefNum, srcDirID, srcName, &finderInfo );
err = HOpen( srcVRefNum, srcDirID, srcName, fsRdPerm, &srcRefNum );
if( err ) return( err );
err = HDelete( dstVRefNum, dstDirID, dstName );
err = HCreate( dstVRefNum, dstDirID, dstName,
finderInfo.fdCreator, finderInfo.fdType );
if( err ) return( err );
err = HOpen( dstVRefNum, dstDirID, dstName, fsWrPerm, &dstRefNum );
if( err )
{
FSClose( srcRefNum );
return( err );
}
err = CopyFork( srcRefNum, dstRefNum );
err = FSClose( srcRefNum );
err = FSClose( dstRefNum );
err = HOpenRF( srcVRefNum, srcDirID, srcName, fsRdPerm, &srcRefNum );
if( err ) return( err );
err = HOpenRF( dstVRefNum, dstDirID, dstName, fsWrPerm, &dstRefNum );
if( err )
{
FSClose( srcRefNum );
return( err );
}
err = CopyFork( srcRefNum, dstRefNum );
err = FSClose( srcRefNum );
err = FSClose( dstRefNum );
return( err );
}
/*******************************************************************/
OSErr CopyDir( char *srcName, char *dstName,
int16 srcVRefNum, int16 dstVRefNum,
int32 srcParDirID, int32 dstDirID )
{
OSErr err;
int32 newDirID, srcDirID;
int16 entries;
CInfoPBRec scanPB;
char scan[ 256 ];
/* create new directory */
CtoPstr( dstName );
err = DirCreate( dstVRefNum, dstDirID, dstName, &newDirID );
if( err )
return( err );
/* get number of entries */
CtoPstr( srcName );
scanPB.dirInfo.ioFDirIndex = 0; /* this directory */
scanPB.dirInfo.ioCompletion = 0L;
scanPB.dirInfo.ioNamePtr = (StringPtr) srcName;
scanPB.dirInfo.ioVRefNum = srcVRefNum;
scanPB.dirInfo.ioDrDirID = srcParDirID;
err = PBGetCatInfo( &scanPB, FALSE );
if( err )
return( err );
srcDirID = scanPB.dirInfo.ioDrDirID;
entries = scanPB.dirInfo.ioDrNmFls + 1;
/*
* for each entry
* if a file then
* copy file
* else if a directory then
* copy dir
*/
while( entries-- )
{
CursorWait();
if( abortFileOp || UserAbort() )
{
abortFileOp = TRUE;
break;
}
scanPB.hFileInfo.ioFDirIndex = entries;
scanPB.hFileInfo.ioCompletion = 0L;
scanPB.hFileInfo.ioNamePtr = (StringPtr) scan;
scanPB.hFileInfo.ioVRefNum = srcVRefNum;
scanPB.hFileInfo.ioDirID = srcDirID;
err = PBGetCatInfo( &scanPB, FALSE );
if( err )
break;
PtoCstr( scan );
if( scanPB.hFileInfo.ioFlAttrib & 0x10 ) /* was a directory */
err = CopyDir( scan, scan, srcVRefNum, dstVRefNum, srcDirID, newDirID );
else
err = CopyFile( scan, scan, srcVRefNum, dstVRefNum, srcDirID, newDirID );
if( err )
break;
}
SetArrow();
return( err );
}
/*******************************************************************/
OSErr MoveFile( char *srcName, char *dstName,
int16 srcVRefNum, int16 dstVRefNum,
int32 srcDirID, int32 dstDirID )
{
OSErr err;
if( srcDirID == dstDirID ) /* rename ? */
{
if( strcmp( srcName, dstName ) == 0 ) /* ? */
return( noErr );
CtoPstr( srcName );
CtoPstr( dstName );
err = HRename( srcVRefNum, srcDirID, srcName, dstName );
}
else if( srcVRefNum != dstVRefNum ) /* from one volume to another */
{
err = CopyFile( srcName, dstName, srcVRefNum, dstVRefNum, srcDirID, dstDirID );
if( !err )
err = HDelete( srcVRefNum, srcDirID, srcName );
}
else /* its a move */
{
CtoPstr( srcName );
err = GetDirName( dstVRefNum, dstDirID, dstName );
err = GetParID( dstVRefNum, dstDirID, &dstDirID );
err = CatMove( srcVRefNum, srcDirID, srcName, dstDirID, dstName );
}
return( err );
}
/*******************************************************************/
OSErr MoveDir( char *srcName, char *dstName,
int16 srcVRefNum, int16 dstVRefNum,
int32 srcDirID, int32 dstDirID )
{
OSErr err;
if( srcDirID == dstDirID ) /* rename ? */
{
if( strcmp( srcName, dstName ) == 0 ) /* ? */
return( noErr );
CtoPstr( srcName );
CtoPstr( dstName );
err = HRename( srcVRefNum, srcDirID, srcName, dstName );
}
else if( srcVRefNum != dstVRefNum ) /* from one volume to another */
{
err = CopyDir( srcName, dstName, srcVRefNum, dstVRefNum, srcDirID, dstDirID );
if( !err )
err = RmTree( srcName, srcVRefNum, srcDirID );
}
else /* its a move */
{
CtoPstr( srcName );
err = GetDirName( dstVRefNum, dstDirID, dstName );
err = GetParID( dstVRefNum, dstDirID, &dstDirID );
err = CatMove( srcVRefNum, srcDirID, srcName, dstDirID, dstName );
}
return( err );
}